
package com.wolfheader.wolfgame.service.admin.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.wolfheader.wolfgame.Principal;
import com.wolfheader.wolfgame.bean.admin.AdminBean;
import com.wolfheader.wolfgame.dao.admin.AdminDao;
import com.wolfheader.wolfgame.dao.admin.RoleDao;
import com.wolfheader.wolfgame.entity.admin.AdminEntity;
import com.wolfheader.wolfgame.entity.admin.RoleEntity;
import com.wolfheader.wolfgame.service.admin.AdminService;

/**
 * Service - 管理员
 * 
 * @author 8615 Team
 * @version 1.0
 */
@Service("adminServiceImpl")
public class AdminServiceImpl implements AdminService {

	@Autowired
	private AdminDao adminDao;

	@Autowired
	private RoleDao roleDao;


	@Override
	@Transactional(readOnly = true)
	public boolean authorized() {
		Subject subject = SecurityUtils.getSubject();
		if (subject != null) {
			return subject.isAuthenticated();
		}
		return false;
	}

	@Override
	@Transactional(readOnly = true)
	public List<String> findAuths(Long id) {
		return findAuths(adminDao.findOne(id));
	}

	@Override
	@Transactional(readOnly = true)
	public List<String> findAuths(AdminEntity admin) {
		List<String> auths = new ArrayList<String>();
		if (admin != null) {
			for (RoleEntity role : admin.getRoles()) {
				auths.addAll(role.getPermissionsName());
			}
		}
		return auths;
	}

	@Override
	public Page<AdminEntity> findAll(final String searchKey, Pageable pageable) {
		Page<AdminEntity> page1 = adminDao.findAll(new Specification<AdminEntity>() {
			@Override
			public Predicate toPredicate(Root<AdminEntity> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				// TODO Auto-generated method stub
				Path<String> namePath = root.get("username");
				Path<String> nicknamePath = root.get("realName");
				Path<String> phonePath = root.get("phone");
				Path<String> wxNamePath = root.get("wxName");
				Predicate p = cb.or(cb.like(namePath, "%" + searchKey + "%"),
						cb.like(nicknamePath, "%" + searchKey + "%"), cb.like(phonePath, "%" + searchKey + "%"),
						cb.like(wxNamePath, "%" + searchKey + "%"));
				query.where(p);
				return null;
			}
		}, pageable);
		return page1;
	}

	@Override
	@Transactional(readOnly = true)
	public AdminEntity getCurrent() {
		Long currentId = getCurrentId();
		if (currentId != null) {
			return adminDao.findOne(currentId);
		}
		return null;
	}

	@Override
	@Transactional(readOnly = true)
	public Principal getCurrentPrincipal() {
		Subject subject = SecurityUtils.getSubject();
		if (subject != null) {
			return (Principal) subject.getPrincipal();
		}
		return null;
	}

	@Override
	@Transactional(readOnly = true)
	public Long getCurrentId() {
		Principal currentPrincipal = getCurrentPrincipal();
		if (currentPrincipal != null) {
			return currentPrincipal.getId();
		}
		return null;
	}

	@Override
	public String getCurrentUsername() {
		Principal currentPrincipal = getCurrentPrincipal();
		if (currentPrincipal != null) {
			return currentPrincipal.getUsername();
		}
		return null;
	}

	@Override
	public void add(AdminBean bean) {
		AdminEntity entity = new AdminEntity();
		entity.setUsername(bean.getUsername());
		entity.setPassword(DigestUtils.md5Hex(bean.getPassword()));
		entity.setIdNo(bean.getIdNo());
		entity.setPhone(bean.getPhone());
		entity.setEnabled(bean.getEnabled());
		entity.setWxName(bean.getWxName());
		entity.setRealName(bean.getRealName());
		Set<RoleEntity> list = new HashSet<RoleEntity>();
		for (int i = 0; i < bean.getRoles().size(); i++) {
			list.add(roleDao.findOne(bean.getRoles().get(i)));
		}
		entity.setRoles(list);
		adminDao.save(entity);
		// TODO Auto-generated method stub
	}

	@Override
	public boolean findUsernameEixst(String username) {
		return adminDao.findByUsername(username) == null;
	}
}